package com.shop2cn.iapi.couponmanage.testcase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.common.collect.Lists;
import com.shop2cn.iapi.couponmanage.parameter.GetBatchesByBatchCodesBean;
import com.shop2cn.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.shop2cn.iapi.couponmanage.parameter.old.BatchInfo;
import com.shop2cn.iapi.couponmanage.service.GetBatchesByBatchCodesCall;
import com.shop2cn.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.model.CouponShop2cn.couponbatch;
import com.ymttest.database.model.CouponShop2cn.couponusecondition;
import com.ymttest.database.sqlwapper.CouponShop2cn.couponbatchWapper;
import com.ymttest.database.sqlwapper.CouponShop2cn.couponuseconditionWapper;
import com.ymttest.utils.TestDataManager;
/**
*根据批次号查询优惠券列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetBatchesByBatchCodes{
	private static GetBatchesByBatchCodesBean bean;
	private static GetBatchesByBatchCodesCall call;
	private static SellerCreateBatchBean sellercreatebatchBean=new SellerCreateBatchBean();;
	private static SellerCreateBatchCall sellercreatebatchCall=new SellerCreateBatchCall(); 
	
	private couponbatchWapper couponbatchWapper=new couponbatchWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据批次号查询优惠券列表");
	}

	@Before
	public void caseUp() {
		bean=new GetBatchesByBatchCodesBean();
		call=new GetBatchesByBatchCodesCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchesByBatchCodes_001() {
		Logger.start(true,"根据批次号查询优惠券列表-正常查询-返回成功");
		try {
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchCall =CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			couponbatch batch=couponbatchWapper.selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);
			List<couponbatch> list = new couponbatchWapper().selectBybatchCode(batch.getBatchCode());
			
			bean.setSellerId(sellercreatebatchBean.getSellerId());
			bean.setBatchCodes(Lists.newArrayList(batch.getBatchCode()));
			call.setData(bean);
			call.callService();	
			VerifyReturn(list.get(0));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchesByBatchCodes_003() {
		Logger.start(true,"根据批次号查询优惠券列表-sellerId与batchCodes中含有与买手不对应-返回成功");
		try {
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchCall =CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			couponbatch batch=couponbatchWapper.selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);
			
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchCall =CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			

			
			bean.setSellerId(sellercreatebatchBean.getSellerId());
			bean.setBatchCodes(Lists.newArrayList(batch.getBatchCode()));
			call.setData(bean);
			call.callService();	
			List<BatchInfo> batchInfos = call.getBatchInfo();
			Logger.verifyEquals(0, batchInfos.size(), "验证batchInfos为空");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchesByBatchCodes_004() {
		Logger.start(true,"根据批次号查询优惠券列表-batchCodes中含有不存在-返回失败");
		try {
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchCall =CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			couponbatch batch=couponbatchWapper.selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);
			List<couponbatch> list = new couponbatchWapper().selectBybatchCode(batch.getBatchCode());
			
			bean.setSellerId(sellercreatebatchBean.getSellerId());
			bean.setBatchCodes(Lists.newArrayList(batch.getBatchCode(),"1111"));
			call.setData(bean);
			call.callService();	
			VerifyReturn(list.get(0));				
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchesByBatchCodes_005() {
		Logger.start(true,"根据批次号查询优惠券列表-sellerId中含有存在-返回失败");
		try {
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchCall =CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			couponbatch batch=couponbatchWapper.selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);
			
			bean.setSellerId(sellercreatebatchBean.getSellerId()+1111);
			bean.setBatchCodes(Lists.newArrayList(batch.getBatchCode()));
			call.setData(bean);
			call.callService();	
			List<BatchInfo> batchInfos = call.getBatchInfo();
			Logger.verifyEquals(0, batchInfos.size(), "验证batchInfos为空");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchesByBatchCodes_006() {
		Logger.start(true,"根据批次号查询优惠券列表-batchCode含2条数据-返回成功");
		try {
			 Integer sellerId=Integer.parseInt(TestDataManager.getRandomNum(5));
			 List<String> batchCodes=new ArrayList<String>();
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchBean.setSellerId(sellerId);
			sellercreatebatchCall =CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			couponbatch batch=couponbatchWapper.selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);
			batchCodes.add(batch.getBatchCode());
			
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchBean.setSellerId(sellerId);
			sellercreatebatchCall =CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			batch=couponbatchWapper.selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);
			batchCodes.add(batch.getBatchCode());
			
			bean.setSellerId(sellercreatebatchBean.getSellerId());
			bean.setBatchCodes(batchCodes);
			call.setData(bean);
			call.callService();	
		//	VerifyReturn(list.get(0));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchesByBatchCodes_007() {
		Logger.start(true,"根据批次号查询优惠券列表-sellerId为空-返回成功");
		try {
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchCall =CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			couponbatch batch=couponbatchWapper.selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);
			List<couponbatch> list = new couponbatchWapper().selectBybatchCode(batch.getBatchCode());
			
			//bean.setSellerId(sellercreatebatchBean.getSellerId());
			bean.setBatchCodes(Lists.newArrayList(batch.getBatchCode()));
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("1", call.getCode(), "验证StatusCode为空");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchesByBatchCodes_008() {
		Logger.start(true,"根据批次号查询优惠券列表-batchCode为空-返回成功");
		try {
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchCall =CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			bean.setSellerId(sellercreatebatchBean.getSellerId());
			//bean.setBatchCodes(Lists.newArrayList(batch.getBatchCode()));
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("1", call.getCode(), "验证StatusCode为空");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
private void VerifyReturn(couponbatch item) {
	
	List<BatchInfo> batchInfos = call.getBatchInfo();
	for(int i=0;i<batchInfos.size();i++)
	{
		BatchInfo batchInfo=batchInfos.get(i);
		checkCouponBatch(item,batchInfo);
	    //验证使用条件
	    checkcouponusecondition(item.getBatchId(),batchInfo);
	}

}
private void checkCouponBatch(couponbatch item,BatchInfo batchInfo)
{
	 Logger.comment("====方法：验证数据库checkcouponbatch====");
        Logger.verifyEquals("0", call.getCode(),
                "验证返回code");
     
        Logger.verifyEquals("操作成功！",
        		call.getMessage(), "验证返回Message");


 
         String batchCode = batchInfo.getBatchCode();
         Logger.verifyEquals(item.getBatchId(),
        		batchInfo.getBatchId(),
                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中iBatchId字段相同");
        
        Logger.verifyEquals(item.getUseConditionType(), batchInfo.getUseConditionType(),
				"验证返回字段productLimitType与Ymt_CouponSetting表中UseConditionType一致");
        
        Logger.verifyEquals(item.getBatchCode(),
        		batchInfo.getBatchCode(),
                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中sBatchCode字段相同");
        
        // 验证AcquireStartTime，AcquireEndTime，AcquireCouponMethod
        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:ms"); 
        if(item.getAcquireStartTime()!=null&&batchInfo.getAcquireStartTime()!=null)
        {
        	String acquireStartTime =time.format(item.getAcquireStartTime());
        	Logger.verifyEquals(acquireStartTime.substring(0, 19).replace(" ", "T"),
        		batchInfo.getAcquireStartTime(),
                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中AcquireStartTime字段相同");
        }
        if(item.getAcquireEndTime()!=null&&batchInfo.getAcquireEndTime()!=null)
        {
        	String acquireEndTime =time.format(item.getAcquireEndTime());
        	Logger.verifyEquals(acquireEndTime.substring(0, 19).replace(" ", "T"),
        		batchInfo.getAcquireEndTime(),
                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中AcquireEndTime字段相同");
        }
        // 是否作废
        if(item.getIsInvalid()==null)
        {
        	Logger.verifyEquals(false,
            		batchInfo.getIsExpire(),
                    "验证返回IsCancellation与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                            + "记录中bInvalidStatus字段相同");
        }
        else {
        	Logger.verifyEquals(item.getIsInvalid(),
            		batchInfo.getIsExpire(),
                    "验证返回IsCancellation与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                            + "记录中IsInvalid字段相同");
		}
        
     // 是否领完
        Logger.verifyEquals(item.getTotalNum()==item.getReceivedCount(),
        		batchInfo.getReceiveOver(),
                "验证返回IsReceiveOver正确");
           
   
//        Logger.verifyEquals(item.getSendType(),
//        		batchInfo.getSendType(),
//                "验证返回SendType与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
//                        + "记录中iSendType字段相同");
        
        if(item.getEffectiveType()==null)
        {
        	Logger.verifyEquals(0,
        		batchInfo.getEffectiveType(),
                "验证返回EffectiveType与数据库中表CouponBatch中iEffectiveType字段相同");
        }
        else{
        	   Logger.verifyEquals(item.getEffectiveType(),
               		batchInfo.getEffectiveType(),
                       "验证返回EffectiveType与数据库中表CouponBatch中iEffectiveType字段相同");
        }
        
        if(item.getEffectiveValidType()==null)
        {
        	 Logger.verifyEquals(0,
             		batchInfo.getEffectiveValidType(),
                     "验证返回EffectiveValidType与数据库中表CouponBatch中iEffectiveValidType字段相同");
             }
        else
        {
        	 Logger.verifyEquals(item.getEffectiveValidType(),
             		batchInfo.getEffectiveValidType(),
                     "验证返回EffectiveValidType与数据库中表Ymt_CouponSetting中iEffectiveValidType字段相同");
        
        }

        Logger.verifyEquals(time.format(item.getValidStart()).subSequence(0, 19),
        		batchInfo.getEffectiveStartTime().replace("T", " "),
                "验证返回ValidStart与数据库中表couponbatch中ValidStart字段相同");

        Logger.verifyEquals(time.format(item.getValidEnd()).subSequence(0, 19),
        		batchInfo.getEffectiveEndTime().replace("T", " "),
                "验证返回ValidEnd与数据库中表couponbatch中ValidEnd字段相同");
        
        if(item.getEffectiveDays()==null)
        {
        	Logger.verifyEquals(0,
        		batchInfo.getEffectiveDays(),
                "验证返回iEffectiveDays与数据库中表couponbatch中iEffectiveDays字段相同");
        }
        else {
        	Logger.verifyEquals(item.getEffectiveDays().intValue(),
            		batchInfo.getEffectiveDays(),
                    "验证返回iEffectiveDays与数据库中表couponbatch中iEffectiveDays字段相同");     
		}
        
        Logger.verifyEquals(item.getTotalNum(),
        		batchInfo.getCouponTotalNum(),
                "验证返回TotalCount与数据库中表couponbatch中iCouponTotalNum字段相同");
        
        Logger.verifyEquals(item.getReceivedCount(),
        		batchInfo.getReceivedCount(),
                "验证返回BatchCode与数据库中表couponbatch中ReceivedCount字段相同");
        
        if(item.getIsShowInPage()==null)
        {
        	Logger.verifyEquals(false,
        		batchInfo.getIsShowInPage(),
                "验证返回BatchCode与数据库中表couponbatch中sBatchCode=" + batchCode
                        + "记录中IsShowInPage字段相同");
        }
        else {
            Logger.verifyEquals(item.getIsShowInPage(),
            		batchInfo.getIsShowInPage(),
                    "验证返回BatchCode与数据库中表couponbatch中sBatchCode=" + batchCode
                            + "记录中IsShowInPage字段相同");
		}
        
        Logger.verifyEquals(item.getReceiveUserType(),
        		batchInfo.getUserType(),
                "验证返回BatchCode与数据库中表couponbatch中iUserType字段相同");
        // 验证coupontype-分享券
     //   int couponType=item.getAcquireMethod()==3?2:1;
        Logger.verifyEquals((byte)item.getCouponType(),
        		batchInfo.getCouponType(),
                 "验证返回BatchCode与数据库中表Ymt_CouponBatch中coupontype字段相同");
        
        //活动Id
        Logger.verifyEquals(item.getCouponTypeBizValue(),
        		batchInfo.getCouponTypeBizValue(),
                "验证返回BatchCode与数据库中表couponbatch中sBatchCode=" + batchCode
                        + "记录中CouponTypeBizValue字段相同");
        
        Logger.verifyEquals(item.getEffectiveTimeType(), batchInfo.getEffectiveTimeType(), 
        		"验证返回EffectiveTimeType与数据库中couponbatch中sBatchCode=" + batchCode
        		+"记录中EffectiveTimeType字段相同");
        
        
        
        Logger.verifyEquals(item.getReceiveTimesPerUser(), batchInfo.getReceiveTimesPerUser(),
					"验证返回字段ReceiveNumPerUser与couponbatch表中ReceiveNumPerUser一致");
        
        //币种
        Logger.verifyEquals(item.getCurrency(), batchInfo.getCurrency(), 
        		"验证返回Currency与数据库中couponbatch中sBatchCode=" + batchCode
        		+"记录中Currency字段相同");
        
}
/**
 * 
 * @param batchId
 * @param item 数据库查询couponbatch
 * @param batchInfo call返回的batchInfo
 */
 private void checkcouponusecondition(int batchId,BatchInfo callbatchInfo)
    {
    	 Logger.comment("====方法：验证数据库checkcouponusecondition====");
    	 Logger.comment("########## couponusecondition 限制条件表 #############");
			couponuseconditionWapper userWapper=new couponuseconditionWapper();
			List<couponusecondition> userLst=userWapper.selectByBatchId(batchId);
			
			couponbatch couponbatch= new couponbatchWapper().selectByPrimaryKey(batchId);
			
			if(callbatchInfo.getUseConditionType()==1)
			{
				Logger.verifyEquals(1, userLst.size(), "验证数据库couponusecondition表中的BatchId="+batchId+"的记录中的条数");
				Logger.verifyEquals(1,userLst.get(0).getConditionType(),
						"验证数据库couponusecondition表中BatchId="+ batchId+ "的记录中的ConditionValue()");
				Logger.verifyEquals(couponbatch.getOperatorId().toString(),userLst.get(0).getConditionValue(),
						"验证数据库couponusecondition表中BatchId="+ batchId+ "的记录中的ConditionValue()");
			}	
			else if(callbatchInfo.getUseConditionType()==2)
			{
				Logger.verifyEquals(true, userLst.size()>1, "验证数据库couponusecondition表中的BatchId="+batchId+"的记录中的条数最少2条");
				for(int i=0;i<userLst.size();i++)
				{
					if(userLst.get(i).getConditionType()==1)
					Logger.verifyEquals(couponbatch.getOperatorId().toString(),userLst.get(i).getConditionValue(),
							"验证数据库couponusecondition表中BatchId="+ batchId+ "的记录中的ConditionValue()");
					else if(userLst.get(i).getConditionType()==2)
					{
						if(sellercreatebatchBean.getproductIdList()!=null && sellercreatebatchBean.getproductIdList().size()>0)
						Logger.verifyEquals(true,sellercreatebatchBean.getproductIdList().contains(userLst.get(i).getConditionValue()),
								"验证数据库couponusecondition表中BatchId="+ batchId+ "的记录中的ConditionValue()");
					}
				}
			}
    }
}